﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYBase;
using TYRTUBase.Comm;

namespace TYRTUBase {
    public class DeviceCreator {
        public INI ini { get; private set; }

        public DeviceCreator(INI _ini) {
            ini = _ini;
        }

        public bool Init(Device_IR ir, ref string error) {
            Log.I($"初始化  类型:{ir.EquipType} 设备Id:{ir.EquipId} 名称:{ir.EquipName}");

            string section = ir.EquipId;
            var strcom_ir = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var ip = ini.ReadStringEx(section, $"{EnumConfig_Device.IP}", "");
            var port = ini.ReadIntEx(section, $"{EnumConfig_Device.Port}", 0);

            var para = ir.para;
            para.Com = com;
            para.IP = ip;
            para.Port = port;
            bool isSumulate = ini.ReadBoolEx(section, $"{EnumConfig_Device.IsXMode}", false);
            int irSpd = ini.ReadIntEx(section, $"{EnumConfig_Device.IRSpeed}", 2);
            if (isSumulate) {
                var OCV_Min = ini.ReadDouble(section, $"{EnumConfig_Device.OCVMin}", 0);
                var OCV_Max = ini.ReadDouble(section, $"{EnumConfig_Device.CVMax}", 0);
                var IR_Min = ini.ReadDouble(section, $"{EnumConfig_Device.IRMin}", 0);
                var IR_Max = ini.ReadDouble(section, $"{EnumConfig_Device.IRMax}", 0);
                ir.SetIsXMode(true);
                ir.SetXModePara(new XMode_IR_Para { OCVMin = OCV_Min, OCVMax = OCV_Max, IRMin = IR_Min, IRMax = IR_Max });
            }
            ir.Init();

            if (!ir.Connect()) {
                error = $"内阻仪串口连接失败{strcom_ir}";

                Log.I($"初始化IR 异常 :{error}");
                return false;
            }
            var speed = IRSpedHelper.GetSpeed(irSpd);
            Log.I($"IR 速度  :{speed}");
            if (!ir.InitIR(speed)) {
                error = "初始化IR参数失败!!";
                Log.I($"初始化IR 异常 :{error} ");
                return false;
            }
            error = "OK";
            return true;
        }

        public bool Init(Device_IR_HK ir, ref string error) {
            Log.I($"初始化  类型:{ir.EquipType} 设备Id:{ir.EquipId} 名称:{ir.EquipName}");

            string section = ir.EquipId;
            bool isSumulate = ini.ReadBoolEx(section, $"{EnumConfig_Device.IsXMode}", false);
            int irSpd = ini.ReadIntEx(section, $"{EnumConfig_Device.IRSpeed}", 2);
            if (isSumulate) {
                var OCV_Min = ini.ReadDouble(section, $"{EnumConfig_Device.OCVMin}", 0);
                var OCV_Max = ini.ReadDouble(section, $"{EnumConfig_Device.CVMax}", 0);
                var IR_Min = ini.ReadDouble(section, $"{EnumConfig_Device.IRMin}", 0);
                var IR_Max = ini.ReadDouble(section, $"{EnumConfig_Device.IRMax}", 0);
                ir.SetIsXMode(true);
                ir.SetXModePara(new XMode_IR_Para { OCVMin = OCV_Min, OCVMax = OCV_Max, IRMin = IR_Min, IRMax = IR_Max });
            }
            var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var ip = ini.ReadStringEx(section, $"{EnumConfig_Device.IP}", "");
            var port = ini.ReadIntEx(section, $"{EnumConfig_Device.Port}", 0);

            var para = ir.para;
            para.Com = com;
            para.IP = ip;
            para.Port = port;
            ir.SetPara(para);

            ir.Init();
            if (!ir.Connect()) {
                error = $"内阻仪串口连接失败{com}";

                Log.I($"初始化IR 异常 :{error}");
                return false;
            }
            var speed = IRSpedHelper.GetSpeed(irSpd);
            Log.I($"IR 速度  :{speed}");
            if (!ir.InitIR(speed)) {
                error = "初始化IR参数失败!!";
                Log.I($"初始化IR 异常 :{error}");
                return false;
            }
            error = "OK";
            return true;
        }

        public bool Init(Device_Switch _switch, ref string error) {
            #region 切换板

            Log.I($"初始化  类型:{_switch.EquipType} 设备Id:{_switch.EquipId} 名称:{_switch.EquipName}");
            //var strcom_switcher = ini.ReadInt($"{EnumConfig_Device.com_switch}", 0);

            string section = _switch.EquipId;
            //var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            bool isSumulate = ini.ReadBoolEx(section, $"{EnumConfig_Device.IsXMode}", false);
            if (isSumulate) {
                _switch.SetIsXMode(true);
            }
            var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var ip = ini.ReadStringEx(section, $"{EnumConfig_Device.IP}", "");
            var port = ini.ReadIntEx(section, $"{EnumConfig_Device.Port}", 0);

            var para = _switch.para;
            para.Com = com;
            para.IP = ip;
            para.Port = port;
            _switch.SetPara(para);

            _switch.Init();
            if (!_switch.Connect()) {
                error = $"打开切换板串口失败!请确认串口是否被占用{com}";
                Log.I($"初始化切换板 异常 :{error}");
                return false;
            }
            return true;

            #endregion 切换板
        }

        /// <summary>
        /// 初始化串口参数并且打开串口
        /// </summary>
        /// <param name="scaner"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool Init(Device_Scaner scaner, ref string error) {
            #region 切换板
            Log.I($"初始化  类型:{scaner.EquipType} 设备Id:{scaner.EquipId} 名称:{scaner.EquipName}");
            string section = scaner.EquipId;
            var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var ip = ini.ReadStringEx(section, $"{EnumConfig_Device.IP}", "");
            var port = ini.ReadIntEx(section, $"{EnumConfig_Device.Port}", 0);
            ini.WriteInt(section, $"{EnumConfig_Device.Com}", com);
            ini.WriteStringEx(section, $"{EnumConfig_Device.IP}", ip);
            ini.WriteInt(section, $"{EnumConfig_Device.Port}", port);


            var para = scaner.para;
            para.Com = com;
            para.IP = ip;
            para.Port = port;

            bool isSumulate = ini.ReadBoolEx(section, $"{EnumConfig_Device.IsXMode}", false);
            if (isSumulate) {
                scaner.SetIsXMode(true);
                var startSeq = ini.ReadIntEx(section, $"{EnumConfig_Device.StartSeqNo}", 0);
                var errMaxCount = ini.ReadIntEx(section, $"{EnumConfig_Device.ErrMaxCount}", 0);
                var barCoudeCount = ini.ReadIntEx(section, $"{EnumConfig_Device.BarCodeCount}", 0);
                scaner.SetIsXMode(true);
                scaner.SetXModePara(new XMode_Scaner_Para {
                    DataCount = barCoudeCount,
                    ErrorCount = errMaxCount,
                    StartSeqp = startSeq
                });
            }


            scaner.SetPara(para);
            scaner.Init();
            if (scaner.Connect() == false) {
                error = $"托盘扫码枪{section}串口打开失败!请确认串口是否被占用:{com}";
                return false;
            }
            return true;

            #endregion 切换板
        }

        public bool InitComm(DeviceBase scaner, ref string error) {
            #region 切换板

            Log.I($"初始化  类型:{scaner.EquipType} 设备Id:{scaner.EquipId} 名称:{scaner.EquipName}");
            string section = scaner.EquipId;
            //var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            bool isSumulate = ini.ReadBoolEx(section, $"{EnumConfig_Device.IsXMode}", false);
            if (isSumulate) {
                scaner.SetIsXMode(true);
            }

            var com = ini.ReadIntEx(section, $"{EnumConfig_Device.Com}", 0);
            var ip = ini.ReadStringEx(section, $"{EnumConfig_Device.IP}", "");
            var port = ini.ReadIntEx(section, $"{EnumConfig_Device.Port}", 0);

            var para = scaner.para;
            para.Com = com;
            para.IP = ip;
            para.Port = port;
            scaner.SetPara(para);

            scaner.Init();
            if (scaner.Connect() == false) {
                error = $"设备{scaner.EquipName}串口打开失败!请确认串口是否被占用:{com}";
                return false;
            }
            return true;

            #endregion 切换板
        }
    }
}